package com.terry;

import org.bytedeco.ffmpeg.avcodec.MpegEncContext;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.io.File;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.JProgressBar;

public class Home extends JFrame {

	private JPanel contentPane;
	private JTextField inputRtsp;
	private JTextField inputFileSize;
	private JTextField inputPrefix;
	private JLabel lblIChoose;
	private JTextField fileDir;
	private JButton btnOpen;
	private JLabel lblMb;
	private JLabel errorLab;
	private JLabel label;
	private JButton btnStop;
	private JButton btnStart;

	private static  IRecorder recorder = null;
	
	
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		recorder = new Mp4Recorder();
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Home frame = new Home();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Home() {
		setTitle("视频流拉取保存mp4");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 650, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		JLabel lblRtspInput = new JLabel("rtsp input");
		lblRtspInput.setHorizontalAlignment(SwingConstants.RIGHT);
		lblRtspInput.setBounds(67, 45, 62, 16);
		contentPane.add(lblRtspInput);
		
		inputRtsp = new JTextField();
		inputRtsp.setBounds(141, 40, 407, 26);
		contentPane.add(inputRtsp);
		inputRtsp.setColumns(10);
		
		JLabel lblMaxFilesize = new JLabel("max filesize");
		lblMaxFilesize.setHorizontalAlignment(SwingConstants.RIGHT);
		lblMaxFilesize.setBounds(18, 97, 113, 16);
		contentPane.add(lblMaxFilesize);
		
		JLabel lblPrefixFilename = new JLabel("prefix filename");
		lblPrefixFilename.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPrefixFilename.setBounds(18, 145, 113, 16);
		contentPane.add(lblPrefixFilename);
		
		inputFileSize = new JTextField();
		inputFileSize.setDocument(new NumberLenghtLimitedDmt(10));
		inputFileSize.setColumns(10);
		inputFileSize.setBounds(141, 92, 360, 26);
		
		
		contentPane.add(inputFileSize);
		
		inputPrefix = new JTextField();
		inputPrefix.setColumns(10);
		inputPrefix.setBounds(141, 140, 407, 26);
		contentPane.add(inputPrefix);
		
		btnStart = new JButton("start");
		btnStart.setBounds(18, 243, 117, 29);
		btnStart.addActionListener(new StartActionListener());
		contentPane.add(btnStart);
		
		lblIChoose = new JLabel("dir choose");
		lblIChoose.setHorizontalAlignment(SwingConstants.RIGHT);
		lblIChoose.setBounds(18, 191, 113, 16);
		contentPane.add(lblIChoose);
		
		fileDir = new JTextField();
		fileDir.setColumns(10);
		fileDir.setBounds(141, 186, 293, 26);
		contentPane.add(fileDir);
		
		btnOpen = new JButton("choose");
		btnOpen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser jfc=new JFileChooser();  
		        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY );  
		        jfc.showDialog(new JLabel(), "选择");  
		        File mChooseDir = jfc.getSelectedFile();  
		        if(mChooseDir != null){
					fileDir.setText(mChooseDir.getAbsolutePath());

					System.out.println(mChooseDir.getAbsolutePath());
				}

				
			}
		});
		btnOpen.setBounds(446, 186, 102, 29);
		contentPane.add(btnOpen);
		
		lblMb = new JLabel("MB");
		lblMb.setBounds(517, 97, 31, 16);
		contentPane.add(lblMb);
		
		errorLab = new JLabel("");
		errorLab.setForeground(Color.RED);
		errorLab.setBounds(161, 248, 389, 16);
		contentPane.add(errorLab);
		
		label = new JLabel("*");
		label.setForeground(Color.RED);
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		label.setBounds(36, 45, 25, 16);
		contentPane.add(label);
		
		btnStop = new JButton("stop");
		btnStop.setBounds(151, 243, 117, 29);
		btnStop.setVisible(false);
		btnStop.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				stop();
				
			}
		});
		contentPane.add(btnStop);
		
//		JProgressBar progressBar = new JProgressBar();
//		progressBar.setIndeterminate(true);
//		progressBar.setStringPainted(true);//显示信息
//		progressBar.setString("录制中...");//提示信息
//		progressBar.setBounds(28, 219, 520, 20);
//		contentPane.add(progressBar);
	}





	private void start(SettingInfo info) {
		recorder.start(info,new InnerRecorderListener());
		btnStop.setVisible(true);
		btnStart.setEnabled(false);
		btnStart.setText("pulling");
	}


	private void stop() {
		recorder.stop();
		handlerStopUI();
	}

	private void handlerStopUI(){
		btnStop.setVisible(false);
		btnStart.setEnabled(true);
		btnStart.setText("start");
	}

	private boolean checkFileDir(String filePath) {
		if(!filePath.contains(File.separator)) return false;
		File file = new File(filePath);
		if(file.exists() && file.isDirectory()) return true;
		if(!file.exists() && file.mkdirs()) return true;
		return false;
	}
	
	
	private class StartActionListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			errorLab.setText("");
			SettingInfo info = null;//new SettingInfo(fileMaxSizeInByte, inputPrefix.getText(), cho, rtspFilePath)
			
			if(JarTools.isEmpty(inputRtsp.getText())) {
				errorLab.setText("Please input the rtsp!");
				return;
			}
			
			
			int fileMaxSizeInByte = 0;
			// 单个文件 最大大小
			int fileMaxSize = JarTools.convertInt(inputFileSize.getText());
			if(fileMaxSize <= 0 ) {
				fileMaxSize = 0 ;
			}else {
				fileMaxSizeInByte = fileMaxSize * 1000 * 1000;
			}
			
			
			
			String chooseDir = fileDir.getText();
			if(!JarTools.isEmpty(chooseDir)) {
				if(checkFileDir(chooseDir)) {
					info = new SettingInfo(fileMaxSizeInByte, inputPrefix.getText(), chooseDir, inputRtsp.getText());
					start(info);
				}else {
					errorLab.setText("File dir is error,you can choose it by the 'choose' button!");
				}
			}else {
				chooseDir = JarTools.getPath();

				System.out.println("filepath = " +chooseDir);
				info = new SettingInfo(fileMaxSizeInByte, inputPrefix.getText(), chooseDir, inputRtsp.getText());
				start(info);
			}
			
		}
		
	}


	private class InnerRecorderListener implements RecorderListener{

		@Override
		public void onFinish() {
			EventQueue.invokeLater(new Runnable() {


				@Override
				public void run() {
					handlerStopUI();
				}
			});

		}

		@Override
		public void onError(Exception e) {
			EventQueue.invokeLater(new Runnable() {


				@Override
				public void run() {
					handlerStopUI();
					System.err.println(e.toString());

				}
			});
		}
	}
	
	

}
